package com.dw.dwdzcommerce.tools.commons.viewUtil;

import java.util.concurrent.atomic.AtomicInteger;

import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.TranslateAnimation;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.Toast;

public class ViewUtil {
	private static ViewUtil mViewUtil;
	private static NotificationManager manager;

	private ViewUtil() {

	}

	public static ViewUtil getViewUtil() {
		if (mViewUtil == null) {
			synchronized (ViewUtil.class) {
				if (mViewUtil == null) {
					mViewUtil = new ViewUtil();
				}
			}
		}
		return mViewUtil;
	}

	public static int getWinHight(Context context) {
		return context.getResources().getDisplayMetrics().heightPixels;
	}

	public static int getWinWidth(Context context) {
		return context.getResources().getDisplayMetrics().widthPixels;
	}

	/**
	 * 调用系统的截图方法,得到配置好的Intent
	 *
	 * @param uri
	 */
	public static Intent cropBitmap(Uri uri, Context context) {
		Intent intent = new Intent("com.android.camera.action.CROP");
		intent.setDataAndType(uri, "image/*");
		// 下面这个crop=true是设置在开启的Intent中设置显示的VIEW可裁剪
		intent.putExtra("crop", "true");
		// aspectX aspectY 是宽高的比例
		intent.putExtra("aspectX", 1);
		intent.putExtra("aspectY", 1);
		// outputX outputY 是裁剪图片宽高
		intent.putExtra("outputX", 150);
		intent.putExtra("outputY", 150);
		intent.putExtra("return-data", true);
		return intent;
	}

	/**
	 * 创建状态栏提示对象
	 *
	 * @param context
	 * @return
	 */
	public static NotificationManager getNotifyMng(Context context) {
		if (manager != null) {
			return manager;
		} else {
			manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
			return manager;
		}
	}

	public static int getScreenWidth(Context context) {
		int mScreenWidth;
		// 获取屏幕宽度，便于后面动态计算高度
		WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
		DisplayMetrics outMetrics = new DisplayMetrics();
		wm.getDefaultDisplay().getMetrics(outMetrics);
		mScreenWidth = outMetrics.widthPixels;
		return mScreenWidth;
	}

	public static int getScreenHeight(Context context) {
		int mScreenHeight;
		// 获取屏幕宽度，便于后面动态计算高度
		WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
		DisplayMetrics outMetrics = new DisplayMetrics();
		wm.getDefaultDisplay().getMetrics(outMetrics);
		mScreenHeight = outMetrics.heightPixels;
		return mScreenHeight;
	}

	public static Toast showSingleToastShort(Context context, String text) {
		showSingleToast(context, text, Toast.LENGTH_SHORT);
		return overallToast;
	}

	public static Toast showSingleToast(Context context, String text, int time) {
		if (overallToast == null) {
			overallToast = Toast.makeText(context, text, time);
			overallToast.setText(text);
			overallToast.show();
		} else {
			overallToast.cancel();
			overallToast = null;
			showSingleToast(context, text, time);
		}
		return overallToast;
	}

	private static Toast overallToast;// 全局的toast

	public static void hideSoftKeyBoard(EditText et) {
		InputMethodManager imm = (InputMethodManager) et.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
		imm.hideSoftInputFromWindow(et.getWindowToken(), 0);
	}

	public static void setViewVisiable(int VISIABLE, View... views) {
		for (View v : views) {
			v.setVisibility(VISIABLE);
		}
	}

	public static Animation getAlphaAnim() {
		AlphaAnimation alpha = new AlphaAnimation(0.5f, 1);
		alpha.setDuration(50);
		return alpha;
	}

	public static Animation getAlphaAnim(int duration) {
		AlphaAnimation alpha = new AlphaAnimation(0f, 1);
		alpha.setDuration(duration);
		return alpha;
	}

	public static Animation getTrasitionAnim(int i) {
		AnimationSet set = new AnimationSet(true);
		set.setInterpolator(new DecelerateInterpolator());
		TranslateAnimation translateAnimation = new TranslateAnimation(0, 0, 150, 0);
		translateAnimation.setDuration(i);
		Animation alphaAnim = getAlphaAnim(i);
		set.addAnimation(translateAnimation);
		set.addAnimation(alphaAnim);
		set.setFillAfter(true);
		return translateAnimation;
	}

	public static void configViewPager(final ViewPager mVp, final Context context) {
		mVp.post(new Runnable() {

			@Override
			public void run() {
				LayoutParams layoutParams = mVp.getLayoutParams();
				int width = layoutParams.width;
				if (width == LayoutParams.MATCH_PARENT || width == LayoutParams.WRAP_CONTENT) {
					int screenWidth = ViewUtil.getScreenWidth(context.getApplicationContext());
					width = screenWidth;
				}
				int height = (int) (width / 1.6);

				layoutParams.width = width;
				layoutParams.height = height;
				mVp.setLayoutParams(layoutParams);

			}
		});
	}

	public static void configView(final View mVp, final Context context) {
		mVp.post(new Runnable() {

			@Override
			public void run() {
				LayoutParams layoutParams = mVp.getLayoutParams();
				int width = layoutParams.width;
				if (width == LayoutParams.MATCH_PARENT || width == LayoutParams.WRAP_CONTENT) {
					int screenWidth = ViewUtil.getScreenWidth(context.getApplicationContext());
					width = screenWidth;
				}
				int height = (int) (width / 1.6);

				layoutParams.width = width;
				layoutParams.height = height;
				mVp.setLayoutParams(layoutParams);

			}
		});
	}

	private static final AtomicInteger sNextGeneratedId = new AtomicInteger(1);

	/**
	 * Generate a value suitable for use in {@link #setId(int)}. This value will
	 * not collide with ID values generated at build time by aapt for R.id.
	 *
	 * @return a generated ID value
	 */
	public static int generateViewId() {
		for (;;) {
			final int result = sNextGeneratedId.get();
			// aapt-generated IDs have the high byte nonzero; clamp to the range
			// under that.
			int newValue = result + 1;
			if (newValue > 0x00FFFFFF)
				newValue = 1; // Roll over to 1, not 0.
			if (sNextGeneratedId.compareAndSet(result, newValue)) {
				return result;
			}
		}
	}
}
